import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    Fenlei: [],
    list: [],
    listnav: [],
  },
  mutations: {
    get_Fenlei(state, item) {
      state.Fenlei = [...item]
    },
    get_list(state, item,) {
      state.list = [...item]
    },
    get_id(state, item) {
      state.listnav = []
      state.list.forEach(Element => {
        if (Element.cid === item._id) {
          state.listnav.push(Element)
        }
      })
    },
    get_sortprice(state) {

      state.listnav.sort((a, b) => {
        return b.price-a.price
      })

    },
    get_zonghe(state){
      state.listnav.sort((a, b) => {
        return a.price-b.price
      })
    }
  },
  actions: {
    GET_FENLEI(context) {
      axios.get('http://localhost:8080/fenlei.json').then(res => {
        // console.log(res.data.result)
        context.commit('get_Fenlei', res.data.result)
      })
    },
    GET_LIST(context) {
      axios.get('http://localhost:8080/list.json',).then(res => {
        console.log(res.data.result)
        context.commit('get_list', res.data.result)
      })
    },
    GET_id(context, item) {
      context.commit('get_id', item)
    },
    get_SORTprice(context) {
      context.commit('get_sortprice')
    },
    get_ZONGHE(context){
      context.commit('get_zonghe')
    }
  },
  modules: {
  }
})
